﻿@{
    ViewBag.Title = "切换货机";
    Layout = "~/Views/Shared/_layoutPersonal.cshtml";
}
@Styles.Render("~/Content/css/searchVender")

<article class="main">
    <nav class="mod_nav">
        <div class="lay_left">
            <a class="nav_back" href='@Url.Content("~/")Home/Index'><img src="/images/return_1.png" ontouchstart="replaceBackImage(this, 1)" ontouchend="replaceBackImage(this, 2)"></a>
            <span>切换货机</span>
        </div>
    </nav>
    <section class="mod_near">
        <div class="title">附近</div>
        <ul id="nearbyTerminalList"></ul>
    </section>
    <section class="mod_recently">
        <div class="title">最近使用</div>
        <ul id="historyTerminalList">
        </ul>
    </section>    
</article>

<script>
    window.onload = function () {
        TerminalMgr.getCurTerminalNearbyListData(showNearbyList, hideNearbyList);
        UserInfoMgr.getRecentPointListData(showRecentPoint, hideRecentPoint);
    }

    function showNearbyList(nearbyTerminalList) {
        var iLength = 0;
        if (null != nearbyTerminalList) {
            iLength = nearbyTerminalList.length;
        }
        if (0 >= iLength) {
            $(".mod_near").hide();
        } else {
            $(".mod_near").show();

            $("ul.nearbyTerminalList li").each(function () {
                $(this).remove();
            });
            for (var i = 0; i < nearbyTerminalList.length; i++) {
                var $li;
                if (nearbyTerminalList[i].pointinfo.id == TerminalMgr.getTerminalInfo().id) {
                    $li = $('<li>'
                    + '<a class=\"btn_vending\" onclick=\"choosePointAddress(\'' + nearbyTerminalList[i].pointinfo.name + '\',\'' + nearbyTerminalList[i].pointinfo.address + '\',' + nearbyTerminalList[i].pointinfo.gps.latitude + ',' + nearbyTerminalList[i].pointinfo.gps.longitude + ')\" >'
                    + '<span class=\"address1\">' + nearbyTerminalList[i].pointinfo.name + '</span>'
                    + '<span class=\"address2\">' + nearbyTerminalList[i].pointinfo.address + '</span>'
                    + '<span class=\"note\">当前</span>'
                    + '</a>'
                    + '</li>');
                }else {
                    $li = $('<li>'
                    + '<a class=\"btn_vending\" onclick=\"choosePointAddress(\'' + nearbyTerminalList[i].pointinfo.name + '\',\'' + nearbyTerminalList[i].pointinfo.address + '\',' + nearbyTerminalList[i].pointinfo.gps.latitude + ',' + nearbyTerminalList[i].pointinfo.gps.longitude + ')\" >'
                    + '<span class=\"address1\">' + nearbyTerminalList[i].pointinfo.name + '</span>'
                    + '<span class=\"address2\">' + nearbyTerminalList[i].pointinfo.address + '</span>'
                    + '<span class=\"note\">' + nearbyTerminalList[i].distance + 'm</span>'
                    + '</a>'
                    + '</li>');
                }
                $("#nearbyTerminalList").append($li);
            }
        }
    }

    function hideNearbyList() {
        $(".mod_near").hide();
    }

    function showRecentPoint(recentPointList) {
        var iLength = 0;
        if (null != recentPointList) {
            iLength = recentPointList.length;
        }
        if (0 >= iLength) {
            $(".mod_recently").hide();
        } else {
            $(".mod_recently").show();

            $("ul.historyTerminalList li").each(function () {
                $(this).remove();
            });
            for (var i = 0; i < recentPointList.length; i++) {
                var $li;
                if (recentPointList[i].id == TerminalMgr.getTerminalInfo().id) {
                    $li = $('<li>'
                    + '<a class=\"btn_vending\" onclick=\"choosePointAddress(\'' + recentPointList[i].name + '\',\'' + recentPointList[i].address + '\',' + recentPointList[i].gps.latitude + ',' + recentPointList[i].gps.longitude + ')\" >'
                    + '<span class=\"address1\">' + recentPointList[i].name + '</span>'
                    + '<span class=\"address2\">' + recentPointList[i].address + '</span>'
                    + '<span class=\"note\">当前</span>'
                    + '</a>'
                    + '</li>');
                } else {
                    $li = $('<li>'
                    + '<a class=\"btn_vending\" onclick=\"choosePointAddress(\'' + recentPointList[i].name + '\',\'' + recentPointList[i].address + '\',' + recentPointList[i].gps.latitude + ',' + recentPointList[i].gps.longitude + ')\" >'
                    + '<span class=\"address1\">' + recentPointList[i].name + '</span>'
                    + '<span class=\"address2\">' + recentPointList[i].address + '</span>'
                    + '</a>'
                    + '</li>');
                }
                $("#historyTerminalList").append($li);
            }
        }
    }

    function hideRecentPoint() {
        $(".mod_recently").hide();
    }

    function choosePointAddress(name, addr, lat, log) {
        AddressMgr.setCurAddr(name, addr, Number(lat), Number(log));
        window.location.href = '/Home/Index';
    }
</script>
